#include "PatientRecordItem.h"
#include "ui_PatientRecordItem.h"

PatientRecordItem::PatientRecordItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PatientRecordItem)
{
    ui->setupUi(this);
    ui->addr->hide();
    ui->phone->hide();
    ui->birthday->hide();
    ui->number->hide();
}

PatientRecordItem::~PatientRecordItem()
{
    delete ui;
}

void PatientRecordItem::setIndex(int index)
{
    g_index = index;
}

void PatientRecordItem::setNumber(int number)
{
    if (number < 0) {
        ui->number->setText("");
        return;
    }
    ui->number->setText(QString("%1").arg(number));
}

void PatientRecordItem::setId(QString id)
{
    ui->id->setText(id);
}

void PatientRecordItem::setName(QString name)
{
    ui->name->setText(name);
}

void PatientRecordItem::setAge(int age)
{
    if (age <= 0) {
        ui->age->setText("???");
        return;
    }
    ui->age->setText(QString("%1").arg(age));
}

void PatientRecordItem::setBirthday(QString birthday)
{
    ui->birthday->setText(birthday);
}

void PatientRecordItem::setGender(int gender)
{
    if (gender < 0) {
        ui->gender->setText("");
        return;
    }

    ui->gender->setText(QString("%1").arg(gender?tr("男"):tr("女")));
}

void PatientRecordItem::setVisitTime(QString visitTime)
{
    ui->visitTime->setText(visitTime);
}

void PatientRecordItem::setPhone(QString phone)
{
    ui->phone->setText(phone);
}

void PatientRecordItem::setAddr(QString addr)
{
    ui->addr->setText(addr);
}

int PatientRecordItem::getNumber()
{
    if (ui->number->text().isNull() || ui->number->text().isEmpty()) {
        return -1;
    }
    return ui->number->text().toInt();
}

void PatientRecordItem::clear(){
    auto list  = findChildren<QLabel *>();
    for(auto &item:list){
        item->clear();
    }
}

void PatientRecordItem::mousePressEvent(QMouseEvent *ev)
{
    g_mousePos = QPoint(ev->x(), ev->y());

    this->setStyleSheet(QLatin1String("QLabel{\n"
    "background-color: rgb(100, 159, 221);\n"
    "color: rgb(238, 238, 236);\n"
    "border-radius:4px;\n"
    "}"));
}

void PatientRecordItem::mouseReleaseEvent(QMouseEvent *ev)
{
    if (g_mousePos == QPoint(ev->x(), ev->y()))
        emit clickedSignal(g_index);

    this->setStyleSheet(QLatin1String("QLabel{\
                                      background-color: rgb(68, 197,226);\
                                      color: rgb(238, 238, 236);\
                                      border-radius:4px;\
                                      }"));
}
